/// <reference types="vitest" />
import { defineConfig, UserConfig } from 'vite'
import path from 'path'
import dts from 'vite-plugin-dts'
import { name } from './package.json'
import { extend, strToHump } from '@lhy-assist/utils'
import eslint from 'vite-plugin-eslint'

const viteConfig: UserConfig = {
  plugins: [
    eslint({
      include: ['src/**/*.ts', 'src/**/*.vue'],
      fix: true,
    }),
    dts({
      tsConfigFilePath: path.resolve(__dirname, './tsconfig.json'),
      include: ['src/**/*.ts', 'src/**/*.vue'],
      root: `.`,
      outputDir: 'types',
      insertTypesEntry: false,
      copyDtsFiles: false,
      staticImport: false,
      cleanVueFileName: true,
    }),
  ],
  test: {
    include: [`test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}`],
  },
  build: {
    lib: {
      entry: `src/index.ts`,
      name: strToHump(name),
      formats: ['umd', 'es'],
    },
    rollupOptions: {
      input: {
        index: `src/index.ts`,
      },
      output: {
        entryFileNames: `[format]/[name].js`,
        chunkFileNames: `[format]/chunks/[name].js`,
      },
    },
  },
}

export const resolvePackageName = (name: string) =>
  name
    .replace(/@([a-z])/g, (all, letter) => letter.toUpperCase())
    .replace(/\/([a-z])/g, (all, letter) => letter.toUpperCase())
    .replace(/-([a-z])/g, (all, letter) => letter.toUpperCase())
export const extendViteConfig = (config: UserConfig | ((config: UserConfig) => UserConfig)) =>
  defineConfig(extend(true, {}, viteConfig, typeof config === 'function' ? config(viteConfig) : config))
export default viteConfig
